package psl;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.text.SimpleDateFormat;
import java.sql.ResultSet;

/**
 * Created by IntelliJ IDEA.
 * User: phiteshkumar
 * Date: Mar 2, 2007
 * Time: 5:30:00 PM
 */
public class ItemBill extends BasePage
{

    public ItemBill ()
    {
        msViewPage = "ItemBill.jsp";
        msDetailPageId = "loginPage";
        msExtraPageId = "SaleEntry";
    }

    public void setRequest (HashMap fhRequest)
    {
        super.setRequest (fhRequest);
    }

    public void setConstant (HashMap fhRequest)
    {
        String[] laAction = (String[]) fhRequest.get ("action_fld");
        String fsTag = laAction[1];
        if (fsTag.equals ("next"))
        {
            String row_no = (String) fhRequest.get ("row_0");
            if (row_no != null)
            {
                if (!row_no.equals (""))
                {
                    if (!miPreRow.contains (Integer.parseInt (row_no)))
                    {
                        miPreRow.add (Integer.parseInt (row_no));
                    }
                }
            }
        }
        else if (fsTag.equals ("start"))
        {
            miStartRow = 0;
        }
        else if (fsTag.equals ("end"))
        {
        }
        else if (fsTag.equals ("previous"))
        {
            miStartRow = miPreRow.size () < 1 ? 0 : (Integer) miPreRow.get (miPreRow.size () - 1);
            if (miPreRow.size () > 0)
            {
                miPreRow.remove (miPreRow.size () - 1);
            }
        }
        else
        {
            miStartRow = 0;
            moUserSession.msPageID = fsTag;
        }
    }

    public void performUpdate (String[] faRemove, String[] faQuantity, String[] faProId)
    {
        System.err.println ("moUserSession.mvQuantity: " + moUserSession.mvQuantity);
        if (faRemove != null)
        {
            for (int i = 0; i < faRemove.length; i++)
            {
                for (int k = 0; k < faProId.length; k++)
                {
                    String[] laQty = CommonServices.stringToArray (faQuantity[k], ":");
                    String[] laRemove = CommonServices.stringToArray (faRemove[i], ":");
                    if (faProId[k].equals (laRemove[1]))
                    {
                        moUserSession.mvQuantity.remove (laRemove[1]);
                    }
                    else
                    {
                        if (moUserSession.mvQuantity.containsKey (laQty[3]))
                        {
                            if (Float.parseFloat (laQty[1]) < Float.parseFloat (laQty[2]))
                            {
                                moUserSession.msError = "You Can Not Purchase More Then " + laQty[2] + " Item";
                            }
                            else
                            {
                                moUserSession.msError = null;
                                moUserSession.mvQuantity.put (laQty[3], laQty[2]);
                            }
                        }
                    }
                }
            }
        }
        else
        {
            for (int k = 0; k < faProId.length; k++)
            {
                String[] laQty = CommonServices.stringToArray (faQuantity[k], ":");
                if (moUserSession.mvQuantity.containsKey (laQty[3]))
                {
                    if (Float.parseFloat (laQty[1]) < Float.parseFloat (laQty[2]))
                    {
                        moUserSession.msError = "You Can Not Purchase More Then " + laQty[2] + " Item";
                    }
                    else
                    {
                        moUserSession.mvQuantity.put (laQty[3], laQty[2]);
                    }
                }
            }
        }

/*
        String[] lsQtyKey = null;
        String[] lsQtyValue = null;
        ArrayList lvQty = new ArrayList ();
        String[] lsRekey = null;
        String[] lsReValue = null;
        String lsqty = null;
        if (faRemove != null)
        {
            lsRekey = new String[faRemove.length];
            lsReValue = new String[faRemove.length];
            for (int i = 0; i < faRemove.length; i++)
            {
                String[] reQty = CommonServices.stringToArray (faRemove[i], ":");
                lsRekey[i] = reQty[0];
                lsReValue[i] = reQty[1];
            }
        }

        if (faQuantity != null && faQuantity.length != 0)
        {
            lsQtyKey = new String[faQuantity.length];
            lsQtyValue = new String[faQuantity.length];
            boolean flag = false;
            String lsvalue = null;

            for (int i = 0; i < faQuantity.length; i++)
            {
                String[] qty = CommonServices.stringToArray (faQuantity[i], ":");
                lsQtyKey[i] = qty[0];
                if ((int) Float.parseFloat (qty[2]) > (int) Float.parseFloat (qty[1]))
                {
                    flag = true;
                    lsvalue = qty[1];
                    lsQtyValue[i] = 1 + "";
                }
                else
                {
                    lsQtyValue[i] = qty[2];
                }
                if (faRemove != null)
                {
                    for (int j = 0; j < faRemove.length; j++)
                    {
                        if (lsRekey[j].equals (lsQtyKey[i]))
                        {
                            lsqty = null;
                            break;
                        }
                        else
                        {
                            lsqty = lsQtyValue[i];
                        }
                    }
                    if (lsqty != null)
                    {
                        lvQty.add (lsqty);
                    }
                }
                else
                {
                    lvQty.add (lsQtyValue[i]);
                }
            }
            if (flag)
            {
                moUserSession.msError = "You Can Not Purchase More Then " + lsvalue + " Item";
            }
            else
            {
                moUserSession.msError = null;
            }
            Iterator lIterator = lvQty.iterator ();
            String qty[] = new String[lvQty.size ()];
            int i = 0;
            while (lIterator.hasNext ())
            {
                qty[i] = (String) lIterator.next ();
                i++;
            }
            moUserSession.maQuantity = qty;

        }
*/
    }
    public void modifyTempData(HashMap fvQuantity ,ArrayList fvPid)
    {
        String[] lspid = CommonServices.ArrayListToString (fvPid);
        int count = 0;
        try
        {
/*
            String[][] lsMax = DBConn.setRecordsToString ("Select max(to_number(substr(order_no,2))) From tempdata", null, "cid='" + moUserSession.msCustId + "'");
*/
            String lsMax=CommonServices.getMaxNoFormDB("tempdata","order_no","cid='" + moUserSession.msCustId + "'","O");
/*
            int liMax = Integer.parseInt (lsMax[0][0]);
*/
            for (int i = (lspid.length - 1); i >= 0; i--)
            {
                float lfPrice = 0.0f;
                String[][] lsPrice = DBConn.setRecordsToString ("select product_price from product_detail_web", null, "product_id='" + lspid[i] + "'");
                lfPrice = Float.parseFloat(lsPrice[0][0]);
                String lsQuery = "update tempdata set quantity=" + fvQuantity.get (lspid[i])+ " , " +
                        "total =" + lfPrice * Integer.parseInt ((String)fvQuantity.get (lspid[i])) +
                        " where product_id='" + lspid[i] + "' AND cid='" + moUserSession.msCustId + "' " +
                        "AND order_no='O" + lsMax + "'";
                DBConn.executeUpdate (lsQuery);
                count++;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace ();
        }
    }

    public void removeTempData (String fsPid)
    {
        String lsQuery = "Delete from tempdata where product_id='" + fsPid + "'";
        DBConn.executeUpdate (lsQuery);
    }

}
